package it.unibz.easytravel.blogging;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.util.Vector;



import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import it.unibz.easytravel.R;
import it.unibz.easytravel.db.LocationDbAdapter;
import it.unibz.easytravel.db.PlacemarkDbAdapter;
import it.unibz.easytravel.kml.Parser;
import it.unibz.easytravel.zip.ZipManager;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;


public class BloggingActivity extends Activity {
	

	private LocationDbAdapter mLocationDb;
	private PlacemarkDbAdapter mPlacemarkDb;
	private String kmlString = null;
	private static int travel_id;
	private static final String TAG = "EasyTravel Blogging activity";
	 
	
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.blogging_activity_layout);
	
				
		Button button_showtrack = (Button) findViewById(R.id.button_showtrack);
		button_showtrack.setOnClickListener(mShowTrack);
		
		Button button_makekml = (Button) findViewById(R.id.button_makekml);
		button_makekml.setOnClickListener(mMakeKml);
		
		Button button_sendpost = (Button) findViewById(R.id.button_sendpost);
		button_sendpost.setOnClickListener(mSendPost);
 

	}
	


	//stub
	public void SendPost() {
		BloggerAdapter bloggerAdapter = new BloggerAdapter();
		try {
			bloggerAdapter.authenticate("android.bz@gmail.com", "kurmesesam");
			bloggerAdapter.retrieveBlogId();
		} catch (Exception e) {
			Log.e(TAG, "Exception during authentication");
			e.printStackTrace();
		}
	}
	
	protected OnClickListener mSendPost = new OnClickListener() { 
        // @Override 
        public void onClick(View arg0) { 
              
            
             new Thread() { 
                  public void run() { 
                        
                       try {                              
                             
                            SendPost ();
                       } catch (Exception e) { 
                            Log.e("mSendPost", e.toString(), e); 
                       } 
                      
                  } 
             }.start(); 
        } 
   };

	private OnClickListener mShowTrack = new OnClickListener() {
		public void onClick(View v)
		{
			Intent intent = new Intent(BloggingActivity.this, TrackMap.class);
            startActivity(intent);
		}
	};

	private OnClickListener mMakeKml = new OnClickListener() {
		public void onClick(View v) {
			
			
			mLocationDb.open();

//			Cursor locationCursor = mLocationDb.getAllTravelers();
//
//			Vector<GeoPoint> locations = new Vector<GeoPoint>();
//			while (locationCursor.moveToNext()) {
//				locations.addElement(new GeoPoint(
//						(int)(locationCursor.getFloat(locationCursor.getColumnIndexOrThrow("latitude"))*TrackMap.CONSTANT),
//						(int)(locationCursor.getFloat(locationCursor.getColumnIndexOrThrow("longitude"))*TrackMap.CONSTANT))
//				);
//			}
//
//
			//get all the placemarks
			
			mPlacemarkDb.open();

			Cursor placemarkCursor = mPlacemarkDb.getPlacemarks(travel_id);
			Vector<Placemark> placemarks = new Vector<Placemark>();
			while (placemarkCursor.moveToNext()) {
				Placemark p = new Placemark (
						placemarkCursor.getInt(placemarkCursor.getColumnIndexOrThrow("_id")),
						placemarkCursor.getString(placemarkCursor.getColumnIndexOrThrow("name")),
						placemarkCursor.getString(placemarkCursor.getColumnIndexOrThrow("description")),
						placemarkCursor.getString(placemarkCursor.getColumnIndexOrThrow("latitude")),
						placemarkCursor.getString(placemarkCursor.getColumnIndexOrThrow("longitude")),
						placemarkCursor.getString(placemarkCursor.getColumnIndexOrThrow("altitude")),
						placemarkCursor.getString(placemarkCursor.getColumnIndexOrThrow("time")),
						placemarkCursor.getString(placemarkCursor.getColumnIndexOrThrow("picture"))
				);
				placemarks.addElement(p);
				Log.i("MAP", p.id+ " " +p.latitude+" " +p.longitude+" " +p.name+" " +p.description+" ");
			}
			
			kmlString = Parser.CreateKml("kelias", placemarks);
			Log.i("KML", kmlString);
			//FileManager.CreateKmz(kmlString);
			mLocationDb.close();
			mPlacemarkDb.close();
			
			// Make kml file 
			try {                 
	            FileOutputStream fOut = openFileOutput("test.kml", MODE_WORLD_READABLE); 
	            OutputStreamWriter osw = new OutputStreamWriter(fOut);  
	            osw.write(kmlString); 
	            osw.flush(); 
	            osw.close();
			} catch (IOException ioe) { 
	            ioe.printStackTrace(); 
	       }
			
			try {                 
	            FileOutputStream fOut = openFileOutput("test.zip", MODE_WORLD_READABLE); 
	            OutputStreamWriter osw = new OutputStreamWriter(fOut);   
	            osw.flush(); 
	            osw.close();
			} catch (IOException ioe) { 
	            ioe.printStackTrace(); 
	       }
			
			
			//File zipFile = new File("test.zip");
			new ZipManager().zip("test.zip", new File[]{new File("test.kml")}, false, BloggingActivity.this);
			Toast.makeText(BloggingActivity.this, "Make KML file", Toast.LENGTH_SHORT).show();
		}
	};

}
